package com.twu.biblioteca.Models;

import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: Nimrod
 * Date: 1/26/13
 * Time: 12:39 AM
 * Represents the Application's menu and the actions that can be done on it
 */
public class Menu {

    private ArrayList<MenuOption> menuOptions = new ArrayList<MenuOption>();

    public Menu() {
        createMenuItems();
    }

    public void print() {

        System.out.println("\nSelect one of the options below by entering the number corresponding to" +
                "" + " the option you want to use\n");

        for (MenuOption option : menuOptions) {
            System.out.println(option.getId() + " : " + option.getName());
        }

        System.out.println("\n");
    }

    private void createMenuItems() {

        menuOptions.add(new MenuOption(1, "View All Books in Library", false));
        menuOptions.add(new MenuOption(2, "Reserve a Book", true));
        menuOptions.add(new MenuOption(3, "Check Membership details", true));
        menuOptions.add(new MenuOption(4, "View Movies in Library", false));
        menuOptions.add(new MenuOption(0, "Exit Biblioteca", false));
    }

    public boolean isOptionValid(int optionId) {
        return  optionId >= 0 && optionId <= menuOptions.size() - 1;
    }

    public MenuOption parseIntAsMenuOption(int optionId) {
        MenuOption optionToReturn = null;
        if (isOptionValid(optionId)) {
            for (MenuOption option : menuOptions) {
                if (option.getId() == optionId) {
                    optionToReturn = option;
                    break;
                }
            }
        }
        return optionToReturn;
    }

    public ArrayList<MenuOption> getMenuOptions() {
        return menuOptions;
    }

    //Represents a single actionable menu option
    public class MenuOption {

        private int id;
        private String name;
        private boolean requiresLogInToExecute;

        private MenuOption(int id, String name, boolean requiresLogInToExecute) {
            this.id = id;
            this.name = name;
            this.requiresLogInToExecute = requiresLogInToExecute;
        }

        public boolean requiresLogInToExecute() {
            return requiresLogInToExecute;
        }

        public int getId() {
            return id;
        }

        public String getName() {
            return name;
        }
    }
}
